package com.schoolgard.action.device;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.schoolegard.services.RegionsService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.Regions;
import com.schoolgard.entitybean.TreeDataModel;
import com.supersystem.utils.JSONUtils;

public class RegionsAction extends BaseAction{
	private RegionsService regionsService;
	
	private Regions regions;
	
	public void queryList(){
		writeToResponse(JSONUtils.toJSONString(queryCatalogListByParentId(0L)));
	}

	public void saveRegions(){
		  
		String pRid = request.getParameter("pRId");
		String RId = request.getParameter("RId");
		String RpinYin = request.getParameter("RpinYin");
		String RCnName = request.getParameter("RCnName");
		String RLinkUrl = request.getParameter("RLinkUrl");
		String RabbName = request.getParameter("RabbName");
		String RIsleaf = request.getParameter("RIsleaf");
		String RSort = request.getParameter("RSort");
		
		Regions regions = null;
		if(StringUtils.isEmpty(RId)){
			regions = new Regions();
			regions.setParentId(StringUtils.isEmpty(pRid) ? 0 : Long.valueOf(pRid));
		} else {
			regions = regionsService.findById(Long.valueOf(RId));
		}
		regions.setAbbreviation(RabbName);
		regions.setEnable("1");
		regions.setIsleaf(StringUtils.equals(RIsleaf, "1")?true:false);
		regions.setName(RCnName);
		regions.setOrderNo(0);
		regions.setPinYin(RpinYin);
		Long success = regionsService.saveOrUpdate(regions);
		writeToResponse(String.valueOf(success));
	}
	
	public void deleteRegions(){
		String id = request.getParameter("regionsId");
		if(StringUtils.isBlank(id)) return;
		
		int success = delete(Long.valueOf(id));
		writeToResponse(Integer.toString(success)) ;
	}
	
	
	public void findRegions(){
		String id = request.getParameter("regionsId");
		Regions regions = regionsService.findById(Long.valueOf(id));
		writeToResponse(JSONUtils.toJSONString(regions));
	}

	private int delete(Long regionsId){
		Regions regions = regionsService.findById(regionsId);
		List<Regions> children = regionsService.findListByParentId(regions.getId());
		int success = 1; //一个都没有删除则默认删除成功，以便删除空的父节点
		regionsService.delete(regions); //先把自己删掉
		for (Regions child : children) {
			success = delete(child.getId());
			if(success == 0) break;
		}
		return success;
	}
	
	private List<TreeDataModel> queryCatalogListByParentId(Long parentId){
		if(parentId == null){
			parentId = 0L;  //ROOT
		}
		List<TreeDataModel> treeNodes = new ArrayList<TreeDataModel>();
		List<Regions> regionsList = regionsService.findListByParentId(parentId);
		for (Regions reginos : regionsList) {
			TreeDataModel reginosTree = new TreeDataModel();
			reginosTree.setId(reginos.getId().toString());
			reginosTree.setText(reginos.getName());
			List<TreeDataModel> children = queryCatalogListByParentId(reginos.getId());
			
			if(children == null && reginos.getIsleaf()){
				reginosTree.setChildren(null);
			}else{
				reginosTree.setChildren(children);
			}
			treeNodes.add(reginosTree);
		}
		return treeNodes;
	}
	
	public RegionsService getRegionsService() {
		return regionsService;
	}

	public void setRegionsService(RegionsService regionsService) {
		this.regionsService = regionsService;
	}

	public Regions getRegions() {
		return regions;
	}

	public void setRegions(Regions regions) {
		this.regions = regions;
	}

}
